package com.wbao.dianniu.ui;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.LoadingWaitDialog;
import com.wbao.dianniu.customView.NoDoubleClickListener;
import com.wbao.dianniu.data.PartnerDetailData;
import com.wbao.dianniu.listener.IPartnerApplyListener;
import com.wbao.dianniu.listener.IPartnerDetailListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.PartnerApplyManager;
import com.wbao.dianniu.manager.PartnerDetailManager;
import com.wbao.dianniu.update.ErrorDialogHelper;
import com.wbao.dianniu.update.ShareSDKManager;
import com.wbao.dianniu.utils.Notification;

/**
 * 电纽合伙人
 * <p>
 * Created by yejianjun on 2018/5/15.
 */

public class PartnerActivity extends BaseActivity implements IPartnerDetailListener, IPartnerApplyListener {

    private Button rightBtn;
    private TextView exchangeTV, ruleTV, scoreTV, applyTV;
    private Button shareBtn, knowBtn, perfectBtn, addVBtn;
    private PartnerDetailManager manager;

    private int partnerStatus;
    /**
     * 未申请
     */
    private final int STATUS_INIT = 0;
    /**
     * 审批中
     */
    private final int STATUS_ING = 1;
    /**
     * 审批通过
     */
    private final int STATUS_PASS = 2;
    /**
     * 审批不通过
     */
    private final int STATU_FAIL = 3;
    private PartnerApplyManager applyManager;
    private Dialog pDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_partner, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        initView();
        initManger();
        initData();
    }

    private void initView() {
        setTitleName(R.string.dn_partner);
        rightBtn = getRightTextButton();
        rightBtn.setText(getResources().getString(R.string.score_detail));
        rightBtn.setOnClickListener(noDoubleClickListener);

        exchangeTV = (TextView) findViewById(R.id.partner_exchange);
        ruleTV = (TextView) findViewById(R.id.partner_rule);
        scoreTV = (TextView) findViewById(R.id.partner_score_tv);
        applyTV = (TextView) findViewById(R.id.partner_apply_tv);

        shareBtn = (Button) findViewById(R.id.task_share_btn);
        knowBtn = (Button) findViewById(R.id.task_know_btn);
        perfectBtn = (Button) findViewById(R.id.task_perfect_btn);
        addVBtn = (Button) findViewById(R.id.task_addV_btn);

        exchangeTV.setOnClickListener(noDoubleClickListener);
        ruleTV.setOnClickListener(noDoubleClickListener);
        applyTV.setOnClickListener(noDoubleClickListener);
        shareBtn.setOnClickListener(noDoubleClickListener);
        knowBtn.setOnClickListener(noDoubleClickListener);
    }

    private void initManger() {
        manager = new PartnerDetailManager(this);
        manager.addPartnerDetailListener(this);
        applyManager = new PartnerApplyManager(this);
        applyManager.addPartnerApplyListener(this);
        manager.partnerDetail();
    }

    private void initData() {
        if (GlobalContext.getPerfectData()) {
            perfectBtn.setText(getResources().getString(R.string.have_been_distributed));
            perfectBtn.setTextColor(getResources().getColor(R.color.black));
        } else {
            perfectBtn.setOnClickListener(this);
        }

        if (GlobalContext.getIndustryAuthority()) {
            addVBtn.setText(getResources().getString(R.string.have_been_distributed));
            addVBtn.setTextColor(getResources().getColor(R.color.black));
        } else {
            addVBtn.setOnClickListener(this);
        }

    }

    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.partner_apply_tv:
                    applyPartner();
                    break;
                case R.id.partner_exchange:
                    intent(ExchangeActivity.class);
                    break;
                case R.id.task_know_btn:
                    intent(MyKnowledgeActivity.class);
                    break;
                case R.id.right_button:
                    intent(MyScoreActivity.class);
                    break;
                case R.id.partner_rule:
                    intent(PartnerRuleActivity.class);
                    break;
                case R.id.task_share_btn:
                   /* if (!GlobalContext.getPerfectData()) {
                        ErrorDialogHelper.getInstance().showPerfectAuth(PartnerActivity.this);
                        return;
                    }*/
                    ShareSDKManager shareSDKManager = ShareSDKManager.getInstance();
                    shareSDKManager.shareApp(PartnerActivity.this, GlobalContext.getAccountId());
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.task_perfect_btn:
                /** 跳转到完善资料*/
                intent(PerfectInfoActivity.class);
                break;
            case R.id.task_addV_btn:
                if (!GlobalContext.getPerfectData()) {
                    ErrorDialogHelper.getInstance().showPerfectAuth(this);
                } else {
                    /** 跳转到行业认证 */
                    intent(IndustryAuthActivity.class);
                }
                break;
            default:
                break;
        }
    }

    private void intent(Class cls) {
        Intent intent = new Intent();
        intent.setClass(PartnerActivity.this, cls);
        startActivity(intent);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("PartnerActivity"); //统计页面，"MainScreen"为页面名称，可自定义
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("PartnerActivity");
    }

    @Override
    public void onPartnerDetailFailure(int errorCode, String errorMsg) {
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onPartnerDetailSuccess(PartnerDetailData objdata) {
        scoreTV.setText(objdata.getScore() == null ? "0" : objdata.getScore());
        partnerStatus = objdata.getPartnerStatus();

        if (partnerStatus == STATUS_ING) {
            applyTV.setText("审批中");
            applyTV.setClickable(false);
        } else if (partnerStatus == STATUS_PASS) {
            applyTV.setText("一级合伙人");
            applyTV.setClickable(false);
        } else if (partnerStatus == STATU_FAIL) {
            applyTV.setText("未达要求，点击重新申请");
            applyTV.setClickable(true);
        } else {
            applyTV.setText("申请合伙人");
            applyTV.setClickable(true);
        }
    }

    private void applyPartner() {
        showDialog();
    }


    private void showDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setTitle(getResources().getString(R.string.partner_apply_tips));
        builder.setPositiveButton(getResources().getString(R.string.confirm),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        // 提示等待框
                        showLoadingDialog();
                        applyManager.partnerApplay();
                    }
                });
        builder.setNegativeButton(getResources().getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }

    @Override
    public void onPartnerApplyFailure(int errorCode, String errorMsg) {
        cancelLoadingDialog();
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onPartnerApplySuccess() {
        cancelLoadingDialog();
        applyTV.setText("审核中");
        partnerStatus = STATUS_ING;
        applyTV.setClickable(false);
    }

    private void showLoadingDialog() {
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    private void cancelLoadingDialog() {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

}
